import java.util.Random;
import java.util.Scanner;

public class Lab_01 {
    public static void main(String[] args) {
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        int correctCount = 0; // 记录正确数量

        System.out.println("===== 100以内加减法口算练习 =====");
        System.out.println("共50道题，请输入答案（整数）：\n");

        for (int i = 0; i < 50; i++) {
            // 生成两个100以内的随机数
            int num1 = random.nextInt(100);
            int num2 = random.nextInt(100);

            // 随机决定是加法还是减法（0为加法，1为减法）
            int operator = random.nextInt(2);

            int result = 0;
            String operatorStr = "";

            if (operator == 0) {
                // 加法
                result = num1 + num2;
                operatorStr = " + ";
            } else {
                // 减法（确保结果非负，交换两个数）
                if (num1 < num2) {
                    int temp = num1;
                    num1 = num2;
                    num2 = temp;
                }
                result = num1 - num2;
                operatorStr = " - ";
            }

            // 输出题目
            System.out.print("第" + (i + 1) + "题：" + num1 + operatorStr + num2 + " = ");

            // 获取用户输入
            int userAnswer;
            try {
                userAnswer = scanner.nextInt();
            } catch (Exception e) {
                // 处理输入非整数的情况
                System.out.println("输入错误，视为答错");
                scanner.next(); // 清除错误输入
                continue;
            }

            // 判断答案
            if (userAnswer == result) {
                System.out.println("正确！");
                correctCount++;
            } else {
                System.out.println("错误！正确答案是：" + result);
            }
        }

        // 输出最终结果
        System.out.println("\n===== 练习结束 =====");
        System.out.println("总题数：50道");
        System.out.println("做对：" + correctCount + "道");
        System.out.println("做错：" + (50 - correctCount) + "道");
        System.out.println("正确率：" + (correctCount * 100.0 / 50) + "%");

        scanner.close();
    }
}
